package com.neu.cloudFactory.controller;

import com.neu.cloudFactory.PO.Factory;
import com.neu.cloudFactory.PO.User;
import com.neu.cloudFactory.VO.RegisterVO;
import com.neu.cloudFactory.VO.UserVO;
import com.neu.cloudFactory.service.FactoryService;
import com.neu.cloudFactory.utils.result.Result;
import com.neu.cloudFactory.utils.result.ResultFactory;
import com.neu.cloudfactory.utils.VerifyCodeUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

import com.neu.cloudFactory.service.UserService;

@RestController
@RequestMapping("/api/user")
public class UserController {
    final UserService userService;
    final FactoryService factoryService;

    public UserController(UserService userService, FactoryService factoryService) {
        this.userService = userService;
        this.factoryService = factoryService;
    }

    @CrossOrigin
    @PostMapping(value = "/login")
    @ResponseBody
    public Result logInCheck(@RequestBody User requestUser) {
        System.out.println(requestUser.toString());
        String account = requestUser.getAccount();
        String password = requestUser.getPassword();

        if (!userService.isExist(account)) {
            return ResultFactory.buildFailResult("用户不存在");
        } else if (!(userService.checkPassword(account, password))) {
            return ResultFactory.buildFailResult("密码不正确");
        } else {
            UserVO userVO = new UserVO(userService.findUserToLogIn(account, password));
            return ResultFactory.buildSuccessResult(userVO, "登录成功");
        }
    }

    @CrossOrigin
    @PostMapping(value = "/register")
    @ResponseBody
    public Result register(@RequestBody RegisterVO registerVO) {

        if (userService.isExist(registerVO.getAccount())) {
            return ResultFactory.buildFailResult("用户已存在");
        } else {
            //保存user
            User user = new User();
            user.setAccount(registerVO.getAccount());
            user.setPassword(registerVO.getPassword());
            user.setName(registerVO.getName());
            user.setPhone(registerVO.getPhone());
            user.setCharacter(registerVO.getCharacter());
            userService.saveUser(user);

            //获取id
            User newUser = userService.findByAccount(registerVO.getAccount());
            int uId = newUser.getId();

            //保存factory
            Factory factory = new Factory();
            factory.setUserId(uId);
            factory.setName(registerVO.getName());
            factory.setDetail(registerVO.getDetail());
            factory.setState("关停");
            factoryService.addFactory(factory);

            return ResultFactory.buildSuccessResult("注册成功");
        }
    }

    //初始化列表
    @CrossOrigin
    @GetMapping(value = "/getUsers")
    @ResponseBody
    public Result getUsers() {
        List<UserVO> userVOList = userService.findAllUsersVO();
        System.out.println(userVOList.toString());
        return ResultFactory.buildSuccessResult(userVOList);
    }

    //增加用户
    @CrossOrigin
    @PostMapping(value = "/addUser")
    @ResponseBody
    public Result addUser(@RequestBody User requestUser) {
        if (userService.isExist(requestUser.getAccount())) {
            return ResultFactory.buildFailResult("已有该用户");
        } else {
            userService.saveUser(requestUser);
            UserVO userVO = new UserVO(requestUser);
            return ResultFactory.buildSuccessResult(userVO, "添加成功");
        }
    }


    //删除用户
    @CrossOrigin
    @PostMapping(value = "/deleteUser")
    @ResponseBody
    public Result deleteUser(@RequestBody User requestUser) {
        userService.deleteUser(requestUser);
        factoryService.deleteByUserId(requestUser.getId());
        UserVO userVO = new UserVO(requestUser);
        return ResultFactory.buildSuccessResult(userVO, "删除成功");
    }

    //修改用户
    @CrossOrigin
    @PostMapping(value = "/modifyUser")
    @ResponseBody
    public Result modifyUser(@RequestBody User requestUser) {
        userService.saveUser(requestUser);
        System.out.println(requestUser.getCharacter());
        UserVO userVO = new UserVO(requestUser);
        return ResultFactory.buildSuccessResult(userVO, "修改成功");
    }


    @CrossOrigin
    @RequestMapping("/getVerifyCode")
    public void getVerifyCode(HttpSession session, HttpServletResponse response) throws IOException {
        //生成验证码，6位
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        //验证码放入session
        session.setAttribute("verifyCode", verifyCode);
        //验证码存入图片
        response.setContentType("image/png");
        ServletOutputStream outputStream = response.getOutputStream();
        VerifyCodeUtils.outputImage(100, 39, outputStream, verifyCode);
    }

    @GetMapping("/checkVerifyCode")
    public Result checkVerifyCode(String code, HttpSession session) {
        String tmp = (String) session.getAttribute("verifyCode");
        if (tmp == null || tmp.length() == 0) {
            return ResultFactory.buildFailResult("请输入验证码");
        }
        if (tmp.equalsIgnoreCase(code)) {
            return ResultFactory.buildSuccessResult(true);
        } else {
            return ResultFactory.buildFailResult("输入错误，请再输一次");
        }
    }
}
